package com.example.demo.entity;
import lombok.Data;

/**
 * 操作结果
 *
 * @author liudebao
 * @date 2020/1/1
 */
@Data
public class Result {

    public static final Result OK_EMPTY = ok(null);
    /**
     * 状态码
     */
    private String code;

    /**
     * 状态信息
     */
    private String msg;
    /**
     * 状态信息
     */
    private String message;
    /**
     * 集合数据
     */
    private Object data;
    /**
     * 集合数据总条数
     */
    private Long recordsCount;

    /**
     * 判断是否请求成功
     */
    private Boolean success;

    public Result() {}

    /*
    返回数据
     */
    public static Result ok(Object data) {
        return new Result().setCode(ErrorCode.OK).setData(data).setSuccess(true);
    }
    /*
      返回String信息
     */
    public static Result ok(String message) {
        return new Result().setCode(ErrorCode.OK).setMessage(message).setSuccess(true);
    }
    /*
       返回数据和数据总条数
     */
    public static Result ok(Long recordsCount, Object data) {
        return new Result().setCode(ErrorCode.OK).setRecordsCount(recordsCount).setData(data).setSuccess(true);
    }
    /*
       返回错误信息
     */
    public static Result error(String code, String message) {
        return new Result().setCode(code).setMessage(message).setSuccess(false);
    }

    public static Result error(ErrorCode errorCode, String message) {
        return error(errorCode.toString(), message).setSuccess(false);
    }

    public static Result error(ErrorCode.ErrorType type, ErrorCode.ErrorLevel level, String subCode, String message) {
        return error(new ErrorCode(type, level, subCode), message).setSuccess(false);
    }

    public Result setData(Object data) {
        this.data = data;
        return this;
    }

    public Result setCode(String code) {
        this.code = code;
        return this;
    }

    public Result setMessage(String message) {
        this.message = message;
        return this;
    }

    private String service;

    public Result setService(String service) {
        this.service = service;
        return this;
    }

    public Result setRecordsCount(Long recordsCount) {
        this.recordsCount = recordsCount;
        return this;
    }

    public Result setSuccess(boolean success){
        this.success = success;
        return this;
    }

}
